package service.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import service.edu.exception.CustomException;
import service.vod.service.VodService;
import service.vod.utlis.ConstantProperties;
import service.vod.utlis.InitVodUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    //上传视频至阿里云
    @Override
    public String uploadVideo(MultipartFile file) throws IOException {
        /**
         * title:文件上传后的名称
         * fileName：文件上传前的原始名称
         * inputSteam：文件输入流
         */
        String fileName=file.getOriginalFilename();
        assert fileName != null;
        String title=fileName.substring(0,fileName.lastIndexOf("."));//截取视频后缀前的名称作为title
        InputStream inputStream = file.getInputStream();
        UploadStreamRequest request = new UploadStreamRequest(ConstantProperties.KEY_ID, ConstantProperties.KEY_SECRET, title, fileName, inputStream);
        //创建上传器
        UploadVideoImpl uploader = new UploadVideoImpl();
        //返回响应
        UploadStreamResponse response = uploader.uploadStream(request);
        //构建返回内容
        String videoId;
        if (response.isSuccess()){
            videoId=response.getVideoId();
        }else {
            throw new CustomException(555,"视频上传异常");
        }
        return videoId;
    }

    //根据视频id删除阿里云视频
    @Override
    public void deleteVideoById(String videoSourceId) {
        try {
            //初始化对象
            DefaultAcsClient client = InitVodUtil.initVodClient(ConstantProperties.KEY_ID, ConstantProperties.KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request对象内设置视频id,可以删除多个视频,通过逗号隔开
            request.setVideoIds(videoSourceId);
            //调用初始化对象方法实现删除
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new CustomException(555,"视频删除失败");
        }

    }

    @Override
    public void deleteVideoBatch(List<String> videoIdList) {
            try {
                //初始化对象
                DefaultAcsClient client = InitVodUtil.initVodClient(ConstantProperties.KEY_ID, ConstantProperties.KEY_SECRET);
                //创建删除视频request对象
                DeleteVideoRequest request = new DeleteVideoRequest();
                //使用apache的String工具而不是spring的
                String videoIdBatch = StringUtils.join(videoIdList.toArray(), ",");
                request.setVideoIds(videoIdBatch);
                //调用初始化对象方法实现删除
                client.getAcsResponse(request);
            } catch (ClientException e) {
                e.printStackTrace();
                throw new CustomException(555,"视频删除失败");
            }
    }
}
